<template>
  <div class="container">
    <div class="header">
      <div>
        <span class="title">订单状态：</span>
        <el-select v-model="search.status" placeholder="请选择" size="medium" style="width:calc(100% - 100px)">
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>

      <div>
        <span class="title">订单单号：</span>
        <el-input v-model="search.order_sn" placeholder="请输入内容" size="medium" style="width:calc(100% - 100px)" />
      </div>

      <div>
        <span class="title">用户昵称：</span>
        <el-input v-model="search.name" placeholder="请输入内容" size="medium" style="width:calc(100% - 100px)" />
      </div>

      <div>
        <span class="title">用户电话：</span>
        <el-input v-model="search.phone" placeholder="请输入内容" size="medium" style="width:calc(100% - 100px)" />
      </div>

      <div>
        <span class="title">注册时间：</span>
        <el-date-picker
          v-model="timer"
          size="medium"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width:calc(100% - 100px)"
          :clearable="false"
          :picker-options="pickerOptions"
          @change="selectTime"
        />
      </div>

      <div>
        <el-button size="small" type="primary" @click="reset">重置</el-button>
        <el-button size="small" type="primary" @click="getList">搜索</el-button>
      </div>
    </div>

    <div class="content">
      <el-table
        v-loading="loading"
        :data="list"
        :header-cell-style="{'color':'#000'}"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="100"
          align="center"
        />
        <el-table-column
          label="用户信息"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.user" class="user">
              <img :src="scope.row.user.avatar?scope.row.user.avatar:require('@/assets/errImg.png')">
              <div>
                <h4>{{ scope.row.user.nickname || '暂未设置昵称' }}</h4>
                <p>{{ scope.row.user.phone || '-' }}</p>
              </div>
            </div>
            <span v-else>-</span>
          </template>
        </el-table-column>

        <el-table-column
          label="商品信息"
          align="center"
          width="300"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.order_goods" class="goods">
              <img :src="scope.row.order_goods.image?scope.row.order_goods.image:require('@/assets/errImg.png')">
              <div>
                <h4>{{ scope.row.order_goods.name }}</h4>
                <p>
                  <span>￥{{ scope.row.order_goods.price }}</span>
                  <span>，x{{ scope.row.order_goods.num }}</span>
                </p>
              </div>
            </div>
            <span v-else>-</span>
          </template>
        </el-table-column>

        <el-table-column
          label="订单状态"
          width="240"
          align="center"
        >
          <template slot-scope="scope">{{ statusList[scope.row.status].label }}</template>
        </el-table-column>

        <el-table-column
          label="订单单号"
          width="240"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.order_sn || '-' }}</template>
        </el-table-column>

        <el-table-column
          label="订单金额"
          width="150"
          align="center"
        >
          <template slot-scope="scope"><span>{{ scope.row.pay_money || '-' }}元</span></template>
        </el-table-column>

        <el-table-column
          label="订单备注"
          width="200"
          align="center"
        >
          <template slot-scope="scope"><span>{{ scope.row.remarks || '-' }}</span></template>
        </el-table-column>

        <el-table-column
          label="下单时间"
          align="center"
          width="180"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ scope.row.created_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="120">
          <template slot-scope="scope">
            <div class="btn">
              <span @click="deliver(scope.row)">{{ scope.row.status==1?'发货':'-' }} </span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :current-page="currentPage"
          layout="total, sizes, prev, pager, next, jumper"
          :page-size="pagesize"
          :page-sizes="[8, 10, 20, 30]"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog
      title="发货"
      :visible.sync="isDeliver"
      width="30%"
    >

      <div class="orderExpress">
        <span>快递公司：</span>
        <el-select v-model="express_id" placeholder="请选择" style="width:calc(100% - 80px)" size="medium">
          <el-option
            v-for="item in expressList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </div>

      <div class="orderNum">
        <span>快递单号：</span>
        <el-input v-model="no" placeholder="请填写快递单号" style="width:calc(100% - 80px)" size="medium" />
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isDeliver = false">取 消</el-button>
        <el-button type="primary" @click="confimDeliver">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      timer: '',
      statusList: [
        { 'label': '待支付', 'value': '0' },
        { 'label': '待发货', 'value': '1' },
        { 'label': '已发货', 'value': '2' },
        { 'label': '已完成', 'value': '3' },
        { 'label': '已取消', 'value': '4' },
        { 'label': '已关闭', 'value': '5' }
      ],
      search: {
        order_sn: '',
        status: '',
        phone: '',
        name: '',
        start_time: '',
        end_time: ''
      },

      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },

      list: [],
      total: 0,
      pagesize: 8, // 默认一页6条
      currentPage: 1, // 当前页
      isDeliver: false,

      expressList: [],
      orderId: '',
      no: '',
      express_id: ''
    }
  },
  created() {
    this.getList()
    this.getExpress()
  },
  methods: {
    getExpress() {
      this.$request.get('/admin/express/list').then(res => {
        if (res.code == 200) {
          this.expressList = res.data
        }
      }).catch()
    },

    // 重置
    reset() {
      this.currentPage = 1
      this.pagesize = 10

      this.search.order_sn = ''
      this.search.status = ''
      this.search.phone = ''
      this.search.name = ''
      this.search.start_time = ''
      this.search.end_time = ''
      this.timer = ''

      this.list = []
      this.getList()
    },

    // 获取表格数据
    getList() {
      this.loading = true
      this.$request.get('/admin/order', {
        params: {
          ...this.search,
          limit: this.pagesize,
          page: this.currentPage
        }
      }).then(res => {
        this.loading = false
        this.list = res.data.data
        this.total = res.data.total
      }).catch(_ => { this.loading = false })
    },

    // 设置每页条数
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // 分页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 选择时间
    selectTime(e) {
      this.search.start_time = e[0]
      this.search.end_time = e[1]
    },

    // 发货
    deliver(e) {
      if (e.status != 1) return
      this.isDeliver = true
      this.express_id = ''
      this.no = ''
      this.orderId = e.id
    },

    // 确认发货
    confimDeliver() {
      if (!this.express_id) {
        this.$message.warning('请选择快递公司')
        return
      }
      if (!this.no) {
        this.$message.warning('请填写快递单号')
        return
      }
      this.$request.post(`/admin/order/send/${this.orderId}`, {
        express_id: this.express_id,
        no: this.no
      }).then(res => {
        if (res.code == 200) {
          this.$message.success('操作成功')
          this.isDeliver = false
          this.getList()
        }
      }).catch()
    }
  }
}
</script>

<style lang="scss" scoped>
  .header{
    width: 100%;
    height: auto;
    padding: 10px;
    background-color: #fff;
    display: flex;
    flex-wrap: wrap;
    &>div{
      width: calc(100% / 4);;
      display: flex;
      align-items: center;
      margin-right: 30px;
      &:nth-child(4),&:nth-child(5),&:nth-child(6){
        margin-top: 10px;
      }
      &>.title{
        display: inline-block;
        width: 100px;
        font-size: 14px;
      }
    }
  }
  .content{
    width: 100%;
    margin-top: 10px;
  }
  .pagination{
    width: 100%;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }

  .btn{
    &>span{
      display: inline-block;
      color: #1989fa;
      width: 40px;
      text-align: center;
      cursor: pointer;
    }
  }
  .user{
    display: flex;
    align-items: center;
    &>img{
      width: 50px;
      height: 50px;
      border-radius: 50%;
      object-fit: cover;
    }
    &>div{
        margin-left: 10px;
        &>h4{ text-align: left;margin: 0; }
        &>p{ margin: 0; }
    }
  }
  .goods{
    width: 100%;
    display: flex;
    justify-content: space-between;
    &>img{
      width: 70px;
      height: 70px;
      border-radius: 10px;
      object-fit: cover;
    }
    &>div{
      width: calc(100% - 80px);
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      &>h4{ text-align: left;margin: 0; }
      &>p{
        margin: 0;
        display: flex;
      }
    }
  }
  .orderNum,.orderExpress{
    width: 100%;
    display: flex;
    justify-content: space-between;
    &>span{
      width: 80px;
      display: block;
      font-size: 14px;
      color: #333;
      font-weight: bold;
    }
  }
  .orderExpress{
    margin-bottom: 20px;
  }
</style>
